#!/bin/sh
# PCP QA Test No. 1569
# checkout pmlogcompress
#
# Copyright (c) 2024 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check


which pmlogcompress >/dev/null 2>&1 || _notrun "pmlogcompress not installed"

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

# much of this is pandering to older versions of xz where
# --block-size is not supported, and/or the compressed file
# sizes are a little different between xz versions
#
_filter()
{
    sed \
	-e "s@$tmp@TMP@g" \
	-e "s@$here@HERE@g" \
	-e '/+ xz -0 foo/s/0/0 --block-size=10MiB/' \
	-e '/xz_setup: /d' \
	-e '/pcp-meminfo.0.xz/s/1032/892/' \
	-e '/pcp-meminfo.0.xz/s/900/892/' \
	-e '/pcp-meminfo.0.xz/s/964/956/' \
	-e '/pcp-meminfo.meta.xz/s/3644/3640/' \
	-e '/pcp-meminfo.meta.xz/s/3648/3640/' \
	-e '/pcp-meminfo.meta.xz/s/3900/3892/' \
    # end
}

_doit()
{
    echo "--- $* ---" | _filter
    prog=$1
    shift
    $prog "$@" 2>$tmp.err >$tmp.out
    sts=$?
    _filter <$tmp.out
    if [ -s $tmp.err ]
    then
	echo "stderr ..."
	cat $tmp.err | _filter
    fi
    [ $sts != 0 ] && echo "exit status $sts"
}

_check()
{
    if pmlogdump -z "$1" >$tmp.tmp
    then
	diff "$tmp.$1.dump" $tmp.tmp >$tmp.out
	if [ -s $tmp.out ]
	then
	    echo "pmlogdump diffs!!"
	    cat $tmp.out
	fi
    else
	echo "pmlogdump failed!"
    fi
}

# real QA test starts here
echo "=== some error cases ==="
_doit pmlogcompress -x $tmp archives/pcp-meminfo 2>&1 \
| sed -e 's/.*[Ii]llegal/Illegal/' -e 's/ -- c/ -c/'
_doit pmlogdecompress -t $tmp archives/pcp-meminfo
_doit pmlogcompress -NV no-such-archive
mkdir $tmp
touch $tmp/pcp-meminfo.0
_doit pmlogdecompress -t $tmp archives/pcp-meminfo
touch $tmp/pcp-meminfo.index $tmp/pcp-meminfo.meta
_doit pmlogdecompress -V $tmp/pcp-meminfo
_doit pmlogdecompress -c foocompress foobar
_doit pmlogdecompress -A arg1 -A arg2 -A "arg3 and arg4" foobar
_doit pmlogdecompress -f gzip foobar
_doit pmlogdecompress -l 0 foobar
_doit pmlogdecompress -o space foobar
_doit pmlogcompress -o foo foobar
pmlogcp $here/archives/foo+ $tmp
_doit pmlogcompress -l 0 -c no-such-prog $tmp/foo+
_doit pmlogcompress -l 0 -f no-such-prog $tmp/foo+
_doit pmlogcompress -l 0 -c no-such-prog:even-less-likely-prog $tmp/foo+
_doit pmlogcompress -f xz -o space foobar
_doit pmlogcompress -o time -f bzip foobar

echo
echo "=== some decompress success cases ==="
cd $tmp
rm -f *
pmlogcp $here/archives/pcp-meminfo .
pmlogdump -z pcp-meminfo >$tmp.pcp-meminfo.dump
stat --format='%n %s' pcp-meminfo* | _filter
_doit pmlogdecompress -N pcp-meminfo
stat --format='%n %s' pcp-meminfo* | _filter

_doit pmlogdecompress pcp-meminfo
stat --format='%n %s' pcp-meminfo* | _filter
_check pcp-meminfo
rm -f $tmp/*

cd $here
[ -d tmp ] || mkdir tmp
_doit pmlogdecompress -V -t tmp archives/pcp-meminfo
stat --format='%n %s' tmp/pcp-meminfo* | _filter
cd tmp
_check pcp-meminfo
rm -f pcp-meminfo*
cd $here

cd $tmp
rm -f *
_doit pmlogdecompress -t . $here/archives/pcp-meminfo
echo "+++ lzma +++"
xz --format=lzma pcp-meminfo.0
_doit pmlogdecompress -V pcp-meminfo
stat --format='%n %s' pcp-meminfo* | _filter
_check pcp-meminfo
echo "+++ bz2 +++"
bzip2 pcp-meminfo.0
bzip2 pcp-meminfo.meta
# some bzip2 versions choke on files with link
# count > 1, so make a copy of the .index file
#
cp pcp-meminfo.index eek
rm -f pcp-meminfo.index
mv eek pcp-meminfo.index
bzip2 pcp-meminfo.index
_doit pmlogdecompress -V pcp-meminfo
stat --format='%n %s' pcp-meminfo* | _filter
_check pcp-meminfo
echo "+++ bz +++"
bzip2 pcp-meminfo.0
mv pcp-meminfo.0.bz2 pcp-meminfo.0.bz
_doit pmlogdecompress -V pcp-meminfo
stat --format='%n %s' pcp-meminfo* | _filter
_check pcp-meminfo
echo "+++ gz +++"
gzip pcp-meminfo.0
gzip pcp-meminfo.meta
_doit pmlogdecompress -V pcp-meminfo
stat --format='%n %s' pcp-meminfo* | _filter
_check pcp-meminfo
echo "+++ z +++"
gzip pcp-meminfo.0
mv pcp-meminfo.0.gz pcp-meminfo.0.z
_doit pmlogdecompress -V pcp-meminfo
stat --format='%n %s' pcp-meminfo* | _filter
_check pcp-meminfo

echo
echo "=== some compress (and decompress) success cases ==="
pmlogcp $here/archives/foo+ .
stat --format='%n %s' foo+* | _filter
pmlogdump -z foo+ >$tmp.foo+.dump
_doit pmlogcompress -NV -l10240 foo+
_doit pmlogcompress --lower-limit=0 --show-me --verbose foo+
_doit pmlogcompress -l 0 -V -c gzip foo+
stat --format='%n %s' foo+* | _filter
_check foo+
_doit pmlogdecompress foo+
stat --format='%n %s' foo+* | _filter
_doit pmlogcompress -l0 -c no-such-prog -c gzip -c bzip2 -V foo+
stat --format='%n %s' foo+* | _filter
_check foo+
_doit pmlogdecompress foo+
stat --format='%n %s' foo+* | _filter
# -N so no change here
_doit pmlogcompress -l0 -A 'foo' -A '--bar' -A '--fumble' -f xz -N foo+
_doit pmlogcompress -l0 -f xz pcp-meminfo
# skip already compresed files ...
_doit pmlogcompress -l0 -f xz -V pcp-meminfo
stat --format='%n %s' pcp-meminfo* | _filter
_check pcp-meminfo
# args are files, not archive basename
_doit pmlogdecompress pcp-meminfo.meta.xz
stat --format='%n %s' pcp-meminfo* | _filter
_doit pmlogdecompress pcp-meminfo.0.xz blah
stat --format='%n %s' pcp-meminfo* | _filter
_check pcp-meminfo
_doit pmlogcompress -l0 -f xz pcp-meminfo.meta
stat --format='%n %s' pcp-meminfo* | _filter
_doit pmlogcompress -l0 -f xz pcp-meminfo.0 blah
stat --format='%n %s' pcp-meminfo* | _filter
_check pcp-meminfo

# success, all done
exit
